<template>
  <div class="app-container">
    <!--查询-->
    <el-form :inline="true">

      <el-form-item label=" 问题描述">
        <el-input
          v-model="searchForm.questionName"
          placeholder=" 问题描述"
          clearable
          @blur="fetchList"
          @keyup.enter.native="fetchList"
        />
      </el-form-item>
      <search-buttons
        :fetch-data="fetchList"
        :add-fun="addAnswer"
      />
    </el-form>
    <!--表格-->
    <el-table :data="tableData" border stripe class="table">
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="questionName" label="标题" />
      <el-table-column prop="questionLevel" label="难度">
        <template slot-scope="scope">
          <span v-if="scope.row.questionLevel == '0'">简单</span>
          <span v-if="scope.row.questionLevel == '1'">适中</span>
          <span v-if="scope.row.questionLevel == '2'">困难</span>
          <span v-if="scope.row.questionLevel == '4'">地狱</span>
        </template>
      </el-table-column>
      <el-table-column prop="questionTags" label="标签">
        <template slot-scope="scope">
          <el-tag
            v-for="(tag,id) in scope.row.tagList"
            :key="id"
            style="margin: 5px 5px"
          >{{ tag }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="answerMd" label="回答md">
        <template slot-scope="scope">
          <el-link @click="showAnswer(scope.row)">查看答案<i class="el-icon-view el-icon--right" /> </el-link>
        </template>
      </el-table-column>
      <el-table-column prop="useful" label=" 有用数量" />
      <el-table-column prop="useless" label=" 无用数量" />
      <el-table-column label="操作" align="center" width="240">
        <template slot-scope="scope">
          <operation-buttons
            :del-fun="delById"
            :row="scope.row"
            :detail-fun="answerDetail"
            :edit-fun="editAnswer"
          />
        </template>
      </el-table-column>
    </el-table>
    <div>
      <!-- 分页 -->
      <pagination
        :page.sync="searchForm.currentPage"
        :total="total"
        :limit.sync="searchForm.pageSize"
        @pagination="fetchList"
      />
    </div>
    <el-dialog
      :title="question"
      :visible.sync="dialogVisible"
    >
      <el-scrollbar>
        <v-md-editor
          v-model="answerMd"
          mode="preview"
          :disabled-menus="[]"
        />
      </el-scrollbar>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
    </el-dialog>
  </div>
</template>
<script>
import questionAnswer from '@/api/questionAnswer'
import Pagination from '@/components/Pagination/index'
import SearchButtons from '@/components/SearchButtons/index'
import OperationButtons from '@/components/OperationButtons/index'
import {popDialog} from "@/utils/popDialog";
export default {
  components: {
    Pagination,
    SearchButtons,
    OperationButtons
  },
  data() {
    return {
      name: 'questionAnswer',
      searchForm: {
        currentPage: 1,
        pageSize: 10
      },
      total: 0,
      tableData: [],
      question: '',
      answerMd: '',
      dialogVisible: false
    }
  },
  created() {
    this.fetchList()
  },
  methods: {
    fetchList() {
      questionAnswer.fetchList({ ...this.searchForm }).then(resp => {
        this.tableData = resp.data.data
        this.total = resp.data.total
        this.searchForm.pageNum = resp.data.pageNum
        this.searchForm.currentPage = resp.data.currentPage
      })
    },
    delById(id) {
      questionAnswer.delById(id).then(resp => {
        this.$message.info(resp.message)
      })
    },
    clearSearchForm() {
      this.searchForm = {}
    },
    downloadTemplate() {
      window.location.href = 'http://localhost:9527/questionAnswer/template'
    },
    export2Excel() {
      questionAnswer.export2Excel({ ...this.searchForm })
    },
    showAnswer(row) {
      this.dialogVisible = true
      this.answerMd = row.answerMd
      this.question = row.questionName
    },
    addAnswer() {
      this.$router.push('/answer/save')
    },
    editAnswer(id) {
      this.$router.push('/answer/save')
    },
    answerDetail(id) {
      this.$router.push('/answer/save')
    }
  }
}
</script>
<style>
@import "../../styles/dialogForm.scss";
</style>
